<template>
	<a-modal width="540px" v-model:open="showForm" title="编辑管理员" @ok="submit" :confirmLoading="handing">
		<a-form ref="form" :model="formData" :rules="rules" :labelCol="{ span: 5 }" :wrapper-col="{ span: 16, offset: 1 }">
			<a-form-item name="name" label="管理员名称" required>
				<a-input v-model:value="formData.name" />
			</a-form-item>
			<a-form-item name="mobile" label="手机号" required>
				<a-input v-model:value="formData.mobile" />
			</a-form-item>
			<a-form-item name="role_id" label="角色" required>
				<relate-selector v-model:value="formData.role_id" :label="formData.role_name" path="role" />
			</a-form-item>
			<a-form-item name="status" label="状态" required>
				<a-switch v-model:checked="formData.status" checked-children="启用" :checked-value="2" un-checked-children="禁用" :un-checked-value="1" />
			</a-form-item>
			<a-form-item name="avatar" label="头像">
				<avatar-uploader v-model:value="formData.avatar" />
			</a-form-item>
		</a-form>
	</a-modal>
</template>
<script lang="ts">
import {page, form} from "@/mixins"
	
export default {
	mixins: [page, form],
	data() {
		return {
			showForm: false as boolean,
			formPath: 'admin',
			formData: {
				id: undefined,
				name: '',
				mobile: '',
				status: 2,
				avatar: '',
				role_id: 0,
				role_name: '',
				intro: ''
			},
			rules: {
			  name: [{ required: true, trigger: 'change' }],
			  mobile: [{ required: true, trigger: 'change' }],
			  avatar: [{ required: true, trigger: 'change' }],
			  role_id: [{ type: 'number', min: 1, trigger: 'change', message: '角色不能为空' }],
				intro: [{ required: true, trigger: 'change' }],
			}
		}
	},
	methods: {
		add() {
			Object.assign(this.formData, {
				id: undefined,
				name: '',
				mobile: '',
				status: 2,
				avatar: '',
				role_id: 0,
				role_name: '',
				intro: ''
			})
			this.showForm = true;
		},
		edit(record : object) {
			Object.assign(this.formData, record);
			this.showForm = true
		},
		afterSubmit(data: object){
			this.$emit('change', data);
			this.showForm = false
		}
	}
}
</script>